<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Information extends Admin
{
    protected $with = ['user_index'];
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
                ['type' => 'radio', 'name' => '状态', 'key' => 'status',
                    'data'=>[
                        1=>'已处理',
                        2=>'未处理'
                    ]
                ],
            ],
        ];
        if(input('parent')){
            $this->menu_input['all'][0]['select']['where'] = ['key'=>'parent_id','value'=>input('parent')];
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {
        $data = [
            ['field' => 'create_time', 'title' => '日期'],
            ['field' => 'user_index_name', 'title' => '用户昵称'],
            ['field' => 'name', 'title' => '联系人'],
            ['field' => 'tel', 'title' => '联系方式'],
            ['field' => 'status', 'title' => '状态'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    public function index_handle(&$data)
    {
        $data['data'] = array_map(function($v){
            $v['user_index_name'] = $v['user_index']['nickname'];
            if($v['status'] == 1){
                $v['status'] = '已处理';
            }elseif($v['status'] == 2){
                $v['status'] = '未处理';
            }
            return $v;
        },$data['data']);
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
    public function index_where(&$where)
    {
        foreach ($where as $k=>$v){
            if($v[0] == 'time_time'){
                $time = explode('到',$v[2]);
                $where[] = ['create_time','>=',trim($time[0])];
                $where[] = ['create_time','<=',trim($time[1])];
                unset($where[$k]);
            }
        }
        return parent::index_where($where); // TODO: Change the autogenerated stub
    }
    //编辑-数据接口
    public function upload_ajax()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            if ($post) {
            	$data = $this->model->where('id',$post['id'])->find()->toArray();
            	if($data['status'] == 1 && $post['status'] == 2){
            		return msgerror('不能修改状态');
            	}
                $this->model::update($post);
            }
            return datasuccess($post);
        }
    }
}
